package cn.yudao.mqasyn;


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.CountDownLatch2;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

public class producer {

    public static void main(String[] args) throws Exception {
        // 创建连接对象
        DefaultMQProducer producer = new DefaultMQProducer("asyn_producer_group");
        // 连接nameserver
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动
        producer.start();

        CountDownLatch2 countDownLatch2 = new CountDownLatch2(100);
        // 发送信息
        Message message = new Message("asyn", "hello", "李多是吾儿".getBytes(StandardCharsets.UTF_8));
        // 设置延迟10s
        message.setDelayTimeLevel(3);
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });
        // 设置延迟5s
        countDownLatch2.await(5,TimeUnit.SECONDS);
        // 关闭
        producer.shutdown();
    }



}
